'''
Created on 26.7.2011

@author: gilead
'''


from direct.actor.Actor import Actor

from Arc import Arc

from panda3d.core import CollisionTraverser,CollisionNode,CollisionSegment,CollisionHandlerQueue#@UnresolvedImport

class Turret(Actor):
    '''
    classdocs
    '''
    def __init__(self, model, id, beginAngle, endAngle, optimal, falloff):
        '''
        Constructor
        '''
        Actor.__init__(self, model)
        self.setPythonTag('Turret', self) # HUOM: objekti ei havia muistista ennen kuin tama tag poistetaan
        
        self.id = id
        self.beginAngle = beginAngle
        self.endAngle = endAngle
        self.optimal = optimal
        self.falloff = falloff
        
        # liitetaan itseensa turretin kiertosateen ja optimalin ilmaiseva kiekko
        fan = Arc.makeArc(self.beginAngle, self.endAngle, self.optimal)
        min,max = self.getTightBounds()
        size = max - min
        med = min + size/2
        #print "turret loc:", med.getX(), med.getY()
        fan.reparentTo(self)
        fan.setPos(med)
        
        #fan2 = Arc.makeArc2(self.beginAngle, self.endAngle, self.optimal)
        #fan2.reparentTo(self)
        
        self.traverser = CollisionTraverser()            #Make a traverser
        self.queue     = CollisionHandlerQueue()         #Make a handler
        self.pickerNode = CollisionNode('circleRay')
        self.pickerNP = self.attachNewNode(self.pickerNode)
        
        self.line = CollisionSegment()
        self.pickerNode.setIntoCollideMask(0)
        self.line.setPointA(0,0,0)
        self.pickerNode.addSolid(self.line)
        self.traverser.addCollider(self.pickerNP, self.queue)
    
    def getArc(self):
        for turretchild in self.getChildren():
            if turretchild.node().isGeomNode():
                return turretchild
    
    def getTurretPos(self):
        min,max = self.getTightBounds()
        size = max - min
        med = min + size/2
        #return self.getPos() + med
        return med
    
    def getShip(self):
        parent = self.getParent()
        if parent == None:
            return None
        ship = parent.getPythonTag('Ship')
        return ship # joko None tai alus